package com.springbootblog.controller.fontend;

import com.alibaba.druid.pool.DruidDataSource;
import com.springbootblog.utils.CurlRequest;
import com.springbootblog.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 */
@RequestMapping("java")
@RestController
public class TestController
{
    /**
     * 测试线程
     * @throws InterruptedException
     */
    @PostMapping("index/testThread")
    public void testThread() throws InterruptedException
    {


        Thread.sleep(1000);
    }


    @Autowired
    private DataSource dataSource;
    @GetMapping("index/testsss")
    public void testsss()
    {
        // 测试数据库连接池
        DruidDataSource druidDataSource = (DruidDataSource)dataSource;
        System.out.println("最大连接数：" + druidDataSource.getMaxActive());
        System.out.println("初始化连接数：" + druidDataSource.getInitialSize());//*/

        // 测试CURLGET
        // 设置请求头
        /*Map<String, String> getheaders = new HashMap<>();
        getheaders.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
        // 设置请求参数
        Map<String, String> params = new HashMap<>();
        params.put("search", "服务");
        // 获取请求链接
        String appendUrl = CurlRequest.getCurlGetUrl("http://localhost:7001/java/index/getArticleListByCategory?page=1", params);
        System.out.println(appendUrl);
        // 发送请求
        Map<String,Object> postData = CurlRequest.curlGet(appendUrl,getheaders);
        System.out.println(postData);//*/

        // 测试CURLPOST
        // 设置请求头
        /*Map<String, String> headers = new HashMap<>();
        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
        // 设置请求参数
        ArrayList<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("indexName", "test"));
        // 发送请求
        Map<String,Object> s = CurlRequest.curlPost("http://localhost:7001/java/elastic/indexIsExists", list, headers);
        System.out.println(s);//*/

        // 测试ASYNCCURLPOST
        // 发送请求
        Map<String,Object> async = CurlRequest.asyncCurlPost("http://localhost:7001/java/index/tessss", null, null);
        System.out.println(async);
    }

    /**
     * 测试CURL POST异步方法
     */
    @PostMapping("index/tessss")
    public String tessss()
    {
        ArrayList<NameValuePair> asynclist = new ArrayList<>();
        asynclist.add(new BasicNameValuePair("indexName", "test"));
        asynclist.add(new BasicNameValuePair("id", "90"));
        // 设置请求头
        Map<String, String> asyncHeaders = new HashMap<>();
        asyncHeaders.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
        // 发送请求
        Map<String,Object> async = CurlRequest.asyncCurlPost("http://localhost:7001/java/elastic/documentIsExists", asynclist, asyncHeaders);
        System.out.println(async);
        return "我在tessss方法中……";
    }

//    // jwt测试 =========================================================================
//    @Autowired
//    private JwtConfig jwtConfig ;

    /**
     * 用户登录接口
     * @param userName
     * @param passWord
     * @return
     */
    @GetMapping("index/login")
    public Map<String, Object> login (@RequestParam("userName") String userName, @RequestParam("passWord") String passWord){
        // 声明返回map
        Map<String, Object> result = new HashMap<>() ;

//        /** 验证userName，passWord和数据库中是否一致，如不一致，直接return ResultTool.errer(); 【这里省略该步骤】*/
//        String userId = 5 + "";
//        String token = jwtConfig.createToken(userId) ;
//        if (!StringUtils.isEmpty(token)) {
//            result.put("token",token) ;
//        }

        // 声明加密参数map
        Map<String, Object> claims = new HashMap<>();
        claims.put("id",1);
        claims.put("username","camellia");
        claims.put("url","https://guanchao.site");
        claims.put("age",25);
        String token= JwtUtils.createJWT(claims);
        //System.out.println("生成的jwt:"+token);

        Claims paramList = JwtUtils.parseJWT(token);
        System.out.println("解析后的token:"+paramList);
        System.out.println("解析后的token的id"+paramList.get("id"));
        System.out.println("解析后的token的有效期"+paramList.getExpiration());
        System.out.println("解析后的token的url"+paramList.get("url"));
        result.put("code", 200);
        result.put("token", token);
        return result ;
    }

    /**
     * 需要 Token 验证的接口
     */
    /*@PostMapping("index/info")
    public Map<String, Object> info (){
        Map<String, Object> result = new HashMap<>() ;
        result.put("code", 200) ;
        return result;
    }//*/

    /**
     * 根据请求头的token获取userId
     * @param request
     * @return
     */
    /*@GetMapping("index/getUserInfo")
    public Map<String, Object> getUserInfo(HttpServletRequest request){
        String usernameFromToken = jwtConfig.getUsernameFromToken(request.getHeader("token"));
        Map<String, Object> result = new HashMap<>() ;
        result.put("code", 200) ;
        result.put("token", usernameFromToken) ;
        return result;
    }//*/
    // jwt测试 =========================================================================
}
